package com.wwm.springcloud.base.result;

import com.google.common.collect.Maps;
import org.springframework.cglib.beans.BeanMap;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

/**
 * SDK工具类
* @ClassName: ToolUtils 
* @author wwm
* @date 2017年4月17日 上午9:30:59 
*
 */
public class ToolUtils
{
	public static boolean isNull(Object obj)
	{
		boolean isNullFlag = true;
		if (obj != null)
		{
			return isNullFlag;
		}

		if (obj instanceof List)
		{
			isNullFlag = isNull((List) obj);
		}
		else if (obj instanceof Set)
		{
			isNullFlag = isNull((Set) obj);
		}
		else if (obj instanceof Object[])
		{
			isNullFlag = isNull((Object[]) (Object[]) obj);
		}
		else if (obj instanceof Map)
		{
			isNullFlag = isNull((Map) obj);
		}
		else if (obj instanceof String)
		{
			isNullFlag = isNull((String) obj);
		}
		else
		{
			isNullFlag = false;
		}

		return isNullFlag;
	}

	public static boolean isNull(List<?> list)
	{
		return ((list == null) || (list.size() == 0));
	}

	public static boolean isNull(Set<?> set)
	{
		return ((set == null) || (set.size() == 0));
	}

	public static boolean isNull(Object[] objects)
	{
		return ((objects == null) || (objects.length == 0));
	}

	public static boolean isNull(Map<?, ?> map)
	{
		return ((map == null) || (map.isEmpty()));
	}

	public static boolean isNull(String str)
	{
		return ((str == null) || ("".equals(str.trim())) || ("null".equalsIgnoreCase(str.trim())));
	}

	public static String formatNull(Object str)
	{
		return (isNull(str)?"":str.toString());
	}

	public static String getUUID()
	{
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	/**
	 * 获取时间差-防止科学计数法
	 * @param begintime 毫微秒System.nanoTime()
	 * @param endtime 毫微秒
	 * @return
	 */
	public static String getTimeInterval(long endtime,long begintime)
	{
		double hs = ((double)(endtime - begintime))/1000000000;
		BigDecimal bigDecimal = new BigDecimal(hs);
		return bigDecimal.setScale(20,BigDecimal.ROUND_HALF_UP).toString();
	}

	/**
	 * 将对象装换为map
	 * @param bean
	 * @return
	 */
	public static <T> Map<String, Object> beanToMap(T bean) {
		Map<String, Object> map = Maps.newHashMap();
		if (bean != null) {
			BeanMap beanMap = BeanMap.create(bean);
			for (Object key : beanMap.keySet()) {
				map.put(key+"", beanMap.get(key));
			}
		}
		return map;
	}

	/**
	 * 将map装换为javabean对象
	 * @param map
	 * @param bean
	 * @return
	 */
	public static <T> T mapToBean(Map<String, Object> map,T bean) {
		BeanMap beanMap = BeanMap.create(bean);
		beanMap.putAll(map);
		return bean;
	}

	/**
	 * 获取邮件验证码
	 * @return
	 */
	public static String getEmailValidateCode() {
		String[] beforeShuffle = new String[] {
				"1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
				"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
				"a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
				"1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
				"k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
				"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
				"1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
				"u", "v", "w", "x", "y", "z",
				"U", "V", "W", "X", "Y", "Z",
				"1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
		List<String> list = Arrays.asList(beforeShuffle);
		Collections.shuffle(list); // 打乱集合顺序
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < list.size(); i++) {
			sb.append(list.get(i));
		}

		String afterShuffle = sb.toString();
		String result = afterShuffle.substring(0, 8);
		return result;
	}

	/**
	 * 获取ip地址
	 * @param request
	 * @return
	 */
	public static String getIpAddress(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
}
